package com.ruoyi.basics.service.impl;

import java.util.List;

import com.ruoyi.basics.domain.Building;
import com.ruoyi.basics.domain.Community;
import com.ruoyi.basics.domain.Household;
import com.ruoyi.basics.dto.HouseholdRegisterDto;
import com.ruoyi.basics.mapper.HouseholdMapper;
import com.ruoyi.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.basics.mapper.UserMapper;
import com.ruoyi.basics.domain.User;
import com.ruoyi.basics.service.IUserService;

/**
 * 住户管理Service业务层处理
 * 
 * @author kun
 * @date 2022-10-06
 */
@Service
public class UserServiceImpl implements IUserService 
{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private HouseholdMapper householdMapper;
    /**
     * 查询住户管理
     * 
     * @param id 住户管理主键
     * @return 住户管理
     */
    @Override
    public User selectUserById(Long id)
    {
        return userMapper.selectUserById(id);
    }

    /**
     * 查询住户管理列表
     * 
     * @param user 住户管理
     * @return 住户管理
     */
    @Override
    public List<User> selectUserList(User user)
    {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增住户管理
     * 
     * @param user 住户管理
     * @return 结果
     */
    @Override
    public int insertUser(User user)
    {
        return userMapper.insertUser(user);
    }

    /**
     * 修改住户管理
     * 
     * @param user 住户管理
     * @return 结果
     */
    @Override
    public int updateUser(User user)
    {
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除住户管理
     * 
     * @param ids 需要删除的住户管理主键
     * @return 结果
     */
    @Override
    public int deleteUserByIds(Long[] ids)
    {
        return userMapper.deleteUserByIds(ids);
    }

    /**
     * 删除住户管理信息
     * 
     * @param id 住户管理主键
     * @return 结果
     */
    @Override
    public int deleteUserById(Long id)
    {
        return userMapper.deleteUserById(id);
    }

    @Override
    public void phoneRegister(HouseholdRegisterDto householdRegisterDto) {
        String name = householdRegisterDto.getName();
        Long sex = householdRegisterDto.getSex();
        String phone = householdRegisterDto.getPhone();
        String wxnumber = householdRegisterDto.getWxnumber();
        Long communityId = householdRegisterDto.getCommunityId();
        Long buildingId = householdRegisterDto.getBuildingId();
        String address = householdRegisterDto.getAddress();
        String smscode = householdRegisterDto.getSmscode();

        //判断验证码是否过期
        Object codeObj = redisTemplate.opsForValue().get("register"+phone);
        if(codeObj== null){
            throw new BusinessException("手机验证码已过期，请点击重新发送验证码");
        }
        //判断手机验证码是否正确,分割字符串
        String s = codeObj.toString().split(":")[0];
        if (!s.equals(smscode)){
            throw new BusinessException("手机验证码错误");
        }


        adduser(sex, phone, wxnumber, communityId, buildingId, address,name);

        addhousehold(name, phone, communityId, buildingId, address);
    }
    private void adduser(Long sex, String phone, String wxnumber, Long communityId, Long buildingId, String address,String name) {
        User user = new User();
        user.setName(name);
        user.setSex(sex);
        user.setPhone(phone);
        user.setWxnumber(wxnumber);
        user.setCommunityId(communityId);
        user.setBuildingId(buildingId);
        user.setAddress(address);
        user.setPassword("123456");
        userMapper.insertUser(user);
    }

    private void addhousehold(String name, String phone, Long communityId, Long buildingId, String address) {
        Household household = new Household();
        household.setCommunityId(communityId);
        household.setBuildingId(buildingId);
        household.setHouseholdname(name);
        household.setPhone(phone);
        household.setAddress(address);
        household.setStatus(0L);
        householdMapper.insertHousehold(household);
    }
}
